package com.dqhl.smartstable;

import android.app.Activity;
import android.app.Application;

import com.dqhl.smartstable.util.Dlog;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by cheng on 2016/10/11.
 * MyApplication
 */
public class MyApplication extends Application {

    public static List<Activity> activities = new ArrayList<Activity>();
    private static MyApplication instance;

    public static MyApplication getInstance() {
        if (instance == null)
            instance = new MyApplication();
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        initXUtils();
        initJPush();
    }

    /**
     * 初始化 xUtils
     */
    private void initXUtils() {
        x.Ext.init(this);
        x.Ext.setDebug(false); //是否输出debug日志
    }

    /**
     * 初始化 JPush
     */
    private void initJPush() {
        // 设置开启日志,发布时请关闭日志
        JPushInterface.setDebugMode(true);
        // 初始化 JPush
        JPushInterface.init(this);
        Dlog.e("JPush初始化成功---Application");
    }

    /**
     * 添加Activity到容器中
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (!activities.contains(activity))
            activities.add(activity);
    }

    /**
     *  结束指定的Activity
     * @param activity
     */
    public void finishActivity(Activity activity){
        if (activity!=null) {
            this.activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit(){
        for (Activity activity : activities) {
            if (activity!=null) {
                activity.finish();
            }
        }
        System.exit(0);
    }
}
